﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Popup_DispatchReward : Popup_Base
{
	public GameObject[] memberObjectList;
	public UILabel plusLegionExp;
	public UIGrid itemGrid;

	public GameObject noItemObject;

	public Transform memberObjectListTrans;
	public Transform itemObjectListTrans;

    public override void AwakeInit()
    {
    }

    public override void StartInit()
    {

    }

    public override void UpdateScene()
    {
    }

    //public override void UpdateTouch()    // 정의 하면 ButtonTouch() 사용 불가
    //{
    //}

    public override void ButtonTouch(Collider collider)  // 버튼 터치 (터치 시작 끝 같은 오브젝트일때 터치 끝 시점에 호출)
    {
    }

    public override void BackButton()
    {
    }

	public void SetLegionInfos(short plusLegionExp)
	{
		this.plusLegionExp.text = string.Format("+{0}", plusLegionExp);
	}

	public void SetItemInfos(int[][] item, MemberModel[] memberModel)
	{
		if( item.Length == 0 && memberModel.Length == 0 )
		{
			noItemObject.SetActive(true);
			return;
		}

		for(int i=0; i<item.Length; i++)
		{
			if( item[i][0] != 0 )
			{
				GameObject obj = Resources.Load("Prefab/UI/Item") as GameObject;
				DispatchItemCell itemCell = (Instantiate(obj) as GameObject).GetComponent<DispatchItemCell>();
				itemCell.transform.parent = itemGrid.transform;
				itemCell.transform.localScale = Vector3.one;
				itemCell.transform.localPosition = Vector3.zero;
				itemCell.itemName.text = TextController.GetItemName(item[i][0]);
				itemCell.itemAmount.text = string.Format("x {0}", item[i][1]);
                //itemCell.itemImage.spriteName = TextController.GetItemDefine(item[i][0]);
                WeaponAtlasController.SetWeaponSprite(itemCell.itemImage, item[i][0]);

				for(int j=0; j<GameData.Inst.GetItemRarity(item[i][0]); j++)
				{
					itemCell.rareStar[j].SetActive(true);
				}
			}
		}

		foreach(MemberModel member in memberModel)
		{
			GameObject obj = Resources.Load("Prefab/UI/CharItem") as GameObject;
			GameObject itemCell = Instantiate(obj) as GameObject;
			itemCell.transform.parent = itemGrid.transform;
			itemCell.transform.localScale = Vector3.one;
			itemCell.transform.localPosition = new Vector3(0f, 33f, 0f);
		}

		itemGrid.GetComponent<UIGrid>().Reposition();

		if( (item.Length+memberModel.Length) < 7 )
		{
			itemObjectListTrans.localPosition = new Vector3(0f-((((item.Length+memberModel.Length)-1))*165f), 0f, 0f);
		}
	}

	public void SetDispatchMemberInfos(int[][] dispatchMemberList)
	{
		int i=0;
		foreach(int[] memberId in dispatchMemberList)
		{
			foreach(KeyValuePair<string, MemberModel> pair in UserData.Instance.userModel.members_dic)
			{
				if( pair.Key.Equals(memberId[0].ToString()) )
				{
					Transform levelObj = memberObjectList[i].transform.FindChild("level");
					if( levelObj != null )
						levelObj.GetComponent<UILabel>().text = string.Format("LV {0}", pair.Value.level);
					Transform nameObj = memberObjectList[i].transform.FindChild("name");
					if( nameObj != null )
						nameObj.GetComponent<UILabel>().text = TextController.GetStringWithId(string.Format("charName{0}", pair.Value.name));
					Transform expObj = memberObjectList[i].transform.FindChild("exp");
					if( expObj != null )
						expObj.GetComponent<UILabel>().text = string.Format("+{0}", memberId[1]);

					memberObjectList[i++].SetActive(true);
				}
			}
		}

		if( dispatchMemberList.Length < 7 )
		{
			memberObjectListTrans.localPosition = new Vector3(930f-((dispatchMemberList.Length-1)*200f), 0f, 0f);
		}
	}

	public void ClosePopup()
	{
		PopupManager.PopPopup();
	}
}